import Ember from 'ember';

export default Ember.Service.extend({

  _toStringId: function(o) {
    var t = (typeof o),
        iid;
    if (t === "number") {
      return o + '';
    }  else if (t === "object") {
      if (o.get) {
        iid = o.get('id');
      } else if (o.id) {
        iid = o.id;
      } else {
        iid = "-1"
      }
      return iid + "";
    } else {
      return o + '';
    }
  },
  _theyAreArrays: function(a1, a2) {
    var t1 = (typeof a1), t2 = (typeof a2);
    if (t1 !== t2) {
      return false;
    }

    var al1 =  a1.get('length'),
        al2 = a2.get('length');

    if (al1 !== al2) {
      return false;
    }
    return true;
  },
  recordArrayEqual: function(a1, a2) {
    if (!this._theyAreArrays(a1, a2)) {
      return false;
    }
    var self = this;
    return a1.every((it) => {
        return a2.any((it2) => {
          return self.recordEqual(it, it2);
        });
    });
  },
  recordEqual: function(r1, r2) {
    if (!r1 && !r2) {
      return true;
    }
    if ((!r1 && r2) || (r1 && !r2)) {
      return false;
    }
    var sid1 = this._toStringId(r1),
        sid2 = this._toStringId(r2);
    if (sid1 === "-1" || sid2 === "-1") {
      return false;
    } else {
      return sid1 === sid2;
    }
  },
  arrayEqual: function(a1, a2) {
    if (!this._theyAreArrays(a1, a2)) {
      return false;
    }
    return a1.every((it) => {
        return a2.any((it2) => {
          return it === it2;
        });
    });
  }
});
